package models

import (
	log "github.com/sirupsen/logrus"
	"go-admin/global/orm"
	"go-admin/tools"
	"time"
	_ "time"
)

type ZxOrder struct {
	Id             int       `json:"id" gorm:"type:int(11) unsigned;primary_key"`      //
	OrderId        string    `json:"orderId" gorm:"type:varchar(50);"`                 // 订单号
	CustomId       int       `json:"customId,omitempty" gorm:"type:int(11) unsigned;"` // 客户编号
	CustomName     string    `json:"customName,omitempty"`
	CustomTel      string    `json:"customTel,omitempty"`
	CustomAddr     string    `json:"customAddr,omitempty"`
	BuildStrut     string    `json:"buildStrut,omitempty" gorm:"type:varchar(50);"`     // 户型结构
	DecorateType   string    `json:"decorateType,omitempty" gorm:"type:varchar(50);"`   // 装修类型
	DecorateBudget int       `json:"decorateBudget,omitempty" gorm:"type:int(10);"`     // 装修预算
	Date           time.Time `json:"date,omitempty" gorm:"type:timestamp;"`             // 日期
	Schedule       int       `json:"schedule,omitempty" gorm:"type:int(8);"`            // 工期
	SignId         int       `json:"signId,omitempty" gorm:"type:int(11) unsigned;"`    // 签约管理id
	DesignId       int       `json:"designId,omitempty" gorm:"type:int(11) unsigned;"`  // 设计管理id
	BuildId        int       `json:"buildId,omitempty" gorm:"type:int(11) unsigned;"`   // 施工管理id
	RevisitId      int       `json:"revisitId,omitempty" gorm:"type:int(11) unsigned;"` // 回访者id
	Note           string    `json:"note" gorm:"type:varchar(200);"`                    // 备注
	State          string    `json:"state" gorm:"type:varchar(10);"`                    // 状态
	CreateBy       string    `json:"createBy" gorm:"type:varchar(128);"`                // 创建者
	UpdateBy       string    `json:"updateBy" gorm:"type:varchar(128);"`                // 更新者
	DataScope      string    `json:"dataScope" gorm:"-"`
	Params         string    `json:"params"  gorm:"-"`
	BaseModel
}

func (ZxOrder) TableName() string {
	return "zx_order"
}

// 创建ZxOrder
func (e *ZxOrder) Create() (ZxOrder, error) {
	var doc ZxOrder
	if e.Date == tools.GetTime() {
		e.Date = tools.GetCurrntTime()
	}
	result := orm.Eloquent.Table(e.TableName()).Create(&e)
	if result.Error != nil {
		err := result.Error
		return doc, err
	}
	doc = *e
	return doc, nil
}

// 获取ZxOrder
func (e *ZxOrder) Get() (ZxOrder, error) {
	var doc ZxOrder
	table := orm.Eloquent.Table(e.TableName())

	if e.Id != 0 {
		table = table.Where("id = ?", e.Id)
	}

	if e.OrderId != "" {
		table = table.Where("order_id = ?", e.OrderId)
	}

	if e.CustomId != 0 {
		table = table.Where("custom_id like ?", "%"+tools.IntToString(e.CustomId)+"%")
	}

	if err := table.First(&doc).Error; err != nil {
		return doc, err
	}
	return doc, nil
}

// 获取ZxOrder带分页
func (e *ZxOrder) GetPage(pageSize int, pageIndex int) ([]ZxOrder, int, error) {
	var doc []ZxOrder

	table := orm.Eloquent.Select("*").Table(e.TableName())

	//e.CustomId, _ = ZxCustom{Name: e.DesignName}.GetIdByName()
	if e.CustomName != "" {
		table = table.Where("custom_name like ?", "%"+e.CustomName+"%")
	}

	if e.CustomTel != "" {
		table = table.Where("custom_tel = ?", e.CustomTel)
	}

	if e.CustomAddr != "" {
		table = table.Where("custom_addr like ?", "%"+e.CustomAddr+"%")
	}

	//TODO
	//if e.SignState != "" {
	//	table = table.Where("sign_state = ?", e.SignState)
	//}
	//
	//if e.DesignState != "" {
	//	table = table.Where("design_state = ?", e.DesignState)
	//}
	//
	//if e.BuildState != "" {
	//	table = table.Where("build_state = ?", e.BuildState)
	//}
	//
	//if e.RevisitState != "" {
	//	table = table.Where("revisit_state = ?", e.RevisitState)
	//}

	// 数据权限控制(如果不需要数据权限请将此处去掉)
	dataPermission := new(DataPermission)
	dataPermission.UserId, _ = tools.StringToInt(e.DataScope)
	table, err := dataPermission.GetDataScope(e.TableName(), table)
	if err != nil {
		return nil, 0, err
	}
	var count int

	if err := table.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&doc).Error; err != nil {
		return nil, 0, err
	}
	table.Where("`deleted_at` IS NULL").Count(&count)
	return doc, count, nil
}

// 更新ZxOrder
func (e *ZxOrder) Update(id int) (update ZxOrder, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).First(&update).Error; err != nil {
		return
	}

	//参数1:是要修改的数据
	//参数2:是修改的数据
	//if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
	//	log.Error(err)
	//	return
	//}
	updatesMap := tools.C2UpdatesMap(e, update)
	updatesMap["updatedAt"] = e.UpdatedAt
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(updatesMap).Error; err != nil {
		log.Error(err)
		return
	}
	return
}

func (e *ZxOrder) UpdateBySubId(subIdName string, subId int) (update ZxOrder, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where(subIdName+" = ?", subId).First(&update).Error; err != nil {
		return
	}
	//参数1:是要修改的数据
	//参数2:是修改的数据
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
		return
	}

	//updatesMap := tools.ToUpdatesMap(e)
	//updatesMap["updatedAt"] = e.UpdatedAt
	//if err = orm.Eloquent.Table(e.TableName()).Where(subIdName+" = ?", subId).Updates(updatesMap).Error; err != nil{
	//	log.Error(err)
	//	return
	//}
	return
}

func (e *ZxOrder) UpdateBySubId2(subIdName string, subId int, colsName ...string) (update ZxOrder, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where(subIdName+" = ?", subId).First(&update).Error; err != nil {
		return
	}
	//参数1:是要修改的数据
	//参数2:是修改的数据
	//if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(&e).Error; err != nil {
	//	return
	//}

	updatesMap := tools.ToUpdatesMap(e, colsName)
	updatesMap["updatedAt"] = e.UpdatedAt
	if err = orm.Eloquent.Table(e.TableName()).Model(&update).Updates(updatesMap).Error; err != nil {
		log.Error(err)
		return
	}
	return
}

// 删除ZxOrder
func (e *ZxOrder) Delete(id int) (success bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id = ?", id).Delete(&ZxOrder{}).Error; err != nil {
		success = false
		return
	}
	success = true
	return
}

//批量删除
func (e *ZxOrder) BatchDelete(id []int) (Result bool, err error) {
	if err = orm.Eloquent.Table(e.TableName()).Where("id in (?)", id).Delete(&ZxOrder{}).Error; err != nil {
		return
	}
	Result = true
	return
}
